/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jinatonic.confetti.sample.slice;

import com.github.jinatonic.confetti.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.bundle.ElementName;

public class MainAbilitySlice extends AbilitySlice {

    ListContainer listContainer;

    String[] title = new String[]{"Falling confetti from top", "Falling confetti from point", "Explosion", "Shimmering confetti", "Falling ice flakes with touch", "Falling ice flakes with listener"};

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        getWindow().setStatusBarColor(Color.getIntColor("#946335"));
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_container);
        listContainer.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return title.length;
            }

            @Override
            public Object getItem(int i) {
                return i;
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                DirectionalLayout root = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this).parse(
                        ResourceTable.Layout_item_confetti_sample, null, false);
                Text text = (Text) root.findComponentById(ResourceTable.Id_text1);
                text.setText(title[i]);
                return root;
            }
        });

        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent intent1 = new Intent();
                ElementName elementName = new ElementName();
                elementName.setBundleName("com.github.jinatonic.confetti.sample");
                switch (i) {
                    case 0:
                        elementName.setAbilityName(".FallingConfettiFromTopAbility");
                        break;
                    case 1:
                        elementName.setAbilityName(".FallingConfettiFromPointAbility");
                        break;
                    case 2:
                        elementName.setAbilityName(".ExplosionAbility");
                        break;
                    case 3:
                        elementName.setAbilityName(".ShimmeringAbility");
                        break;
                    case 4:
                        elementName.setAbilityName(".FallingWithTouchAbility");
                        break;
                    case 5:
                        elementName.setAbilityName(".FallingConfettiWithListenerAbility");
                        break;
                }
                intent1.setElement(elementName);
                startAbility(intent1);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
